package cn.lili.modules.grab.alibaba.constant;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class GlobalTools {


    public static String getCollectUrl(String key){
        switch(key){
            case "taobao":
                return "https://api03.6bqb.com/taobao/detail";
            case "tmall":
                return "https://api03.6bqb.com/tmall/detail";
            case "jd":
                return "https://api03.6bqb.com/jd/detail";
            case "1688":
                return "https://api03.6bqb.com/alibaba/pro/detail";
        }
        return null;
    }



    public static Map<String,String> getUrlType(String key){
        /**
         *     private $url_type = [
         *         'detail.tmall.com'  =>    ['type' => 'tmall',   'type_name' => '天猫'],
         *         'item.taobao.com'   =>    ['type' => 'taobao',  'type_name' => '淘宝'],
         *         'detail.1688.com'   =>    ['type' => '1688',    'type_name' => '1688'],
         *         'item.jd.com'       =>    ['type' => 'jd',      'type_name' => '京东']
         *     ];
         */
        Map<String,String> urlTypeMap = new HashMap<>();
        switch(key){
            case "detail.tmall.com":
                urlTypeMap.put("type","tmall");
                urlTypeMap.put("type_name","天猫");
                return urlTypeMap;
            case "item.taobao.com":
                urlTypeMap.put("type","taobao");
                urlTypeMap.put("type_name","淘宝");
                return urlTypeMap;
            case "detail.1688.com":
                urlTypeMap.put("type","1688");
                urlTypeMap.put("type_name","1688");
                return urlTypeMap;
            case "item.jd.com":
                urlTypeMap.put("type","jd");
                urlTypeMap.put("type_name","京东");
                return urlTypeMap;
        }
        return null;
    }




    public static Map<String,Object> convertUrlQuery(String $query)
    {
        Map<String,Object> $params = new HashMap<>();
        String[] $queryParts = $query.split("&");
        if($queryParts.length>0){
            Arrays.stream($queryParts).forEach((param)->{
                String[] $item = param.split("=");
                $params.put($item[0],$item[1]);
            });
            return $params;
        }
        return null;
    }



    /**
     * 是否是url链接
     * @param $string
     * @return bool
     */
    public static boolean is_url(String urlStr)
    {
        if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
            return true;
        } else {
            return false;
        }
    }


}
